import { Component, OnInit } from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router'
import axios from 'axios';

@Component({
  selector: 'app-reg',
  templateUrl: './reg.component.html',
  styleUrls: ['./reg.component.css']
})


export class RegComponent implements OnInit {

  constructor(private route: ActivatedRoute, private router: Router) { 
  }

  ngOnInit(): void {
  }


  reg(formobj){
    console.log(formobj.value)

    var params = new URLSearchParams()
    params.append('username', formobj.value.username)
    params.append('password', formobj.value.password)
    params.append('role', formobj.value.role)


    this.getData(params, "/user/reg" );

    
  }

  public async getData(params:URLSearchParams, url:String){
    let baseUrl = "http://localhost:8999";
    await  axios.post(baseUrl+url, params, {withCredentials: true})
    .then((response) => {
        console.log(response);
        if ("success" == response.data){
          this.router.navigate(['/login']);
        }
      return response;
    })
    .catch(function (error) {
      console.log(error);
    });
}

}